<cfcomponent extends="BaseGenerator" displayname="Generates all essential for new Application">
	<cffunction name="init">
		<cfreturn this />
	</cffunction>
	
	<cffunction name="setContext">
		<cfargument name="context_name" required="true" type="String" />
		<cfset setTurbineProperties(arguments.context_name) />
	</cffunction>
	
	<!--- APPLICATION & CONFIG --->
	<cffunction access="public" returntype="void" name="writeApplicationFiles">
		<cfset var context_path = super.getTurbineProperty('ContextPath') />
		<!--- Included template makes use of the FORM scope to write files --->
		<cfsavecontent variable="application_generator"><cfinclude template="templates/application_template.cfm"></cfsavecontent>
		<cfsavecontent variable="config_generator"><cfinclude template="templates/config_template.cfm"></cfsavecontent>
		<cfsavecontent variable="index_generator"><cfinclude template="templates/index_template.cfm"></cfsavecontent>
		<cfsavecontent variable="header_generator"><cfinclude template="templates/layout_header.cfm"></cfsavecontent>
		<cfsavecontent variable="footer_generator"><cfinclude template="templates/layout_footer.cfm"></cfsavecontent>
		<cfsavecontent variable="grids_generator"><cfinclude template="templates/grids-min.css"></cfsavecontent>
		<cftry>
			<cffile action="write" file="#context_path#/Application.cfc" output="#trim(rereplace(rereplace(application_generator,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cfset super.WriteLog("Application", "Successfully written to '#context_path#/Application.cfc'")>
		<cfcatch type="any">
			<cfset super.WriteLog("Application", 'Could not write to "#context_path#/Application.cfc". Detail:#cfcatch.detail# Message:#cfcatch.Message#') />
		</cfcatch>
		</cftry>
				
		<cftry>
			<cffile action="write" file="#context_path#/Config.cfc" output="#trim(rereplace(rereplace(config_generator,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cfset super.WriteLog("Config", "Successfully written to '#context_path#/Config.cfc'")>
		<cfcatch type="any">
			<cfset super.WriteLog("Config", 'Could not write to "#context_path#/Config.cfc". Detail:#cfcatch.detail# Message:#cfcatch.Message#') />
		</cfcatch>
		</cftry>	
	
		<cftry>
			<cffile action="write" file="#context_path#/index.cfm" output="#trim(rereplace(rereplace(index_generator,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cfset super.WriteLog("Index", "Successfully written to '#context_path#/index.cfm'")>
		<cfcatch type="any">
			<cfset super.WriteLog("Index_File", 'Could not write to "#context_path#/index.cfm". Detail:#cfcatch.detail# Message:#cfcatch.Message#') />
		</cfcatch>
		</cftry>
		
		<cftry>
			<cffile action="write" file="#context_path#/app/view/layout/header.cfm" output="#trim(rereplace(rereplace(header_generator,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cffile action="write" file="#context_path#/app/view/layout/footer.cfm" output="#trim(rereplace(rereplace(footer_generator,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cffile action="write" file="#context_path#/public/css/grids.css" output="#trim(rereplace(rereplace(grids_generator,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cfset super.WriteLog("layout_header", "Successfully written to '#context_path#/app/view/layout/header.cfm'") />
			<cfset super.WriteLog("layout_footer", "Successfully written to '#context_path#/app/view/layout/'") />
			<cfset super.WriteLog("basic_css", "Successfully written to '#context_path#/public/css/grids.css'") />
		<cfcatch type="any">
			<cfset super.WriteLog("Application", 'Could not write to "#context_path#/Application.cfc". Detail:#cfcatch.detail# Message:#cfcatch.Message#') />
		</cfcatch>
		</cftry>
		
		
	</cffunction>
	
	<!--- HELPER FILES --->
	<cffunction access="public" returntype="void" name="writeHelper">
		<cfargument name="template_name_and_path" default="templates/helper_template.cfm" type="String" />
		
		<cfsavecontent variable="helper_generator"><cfinclude template="#arguments.template_name_and_path#"></cfsavecontent>
		<cftry>
			<cffile action="write" file="#super.getTurbineProperty('ContextPath')#/app/utils/helper.cfc" output="#trim(rereplace(rereplace(helper_generator,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cfset super.WriteLog("helper", "Successfully written to '#super.getTurbineProperty("ContextPath")#/helper.cfc'")>
		<cfcatch type="any">
			<cfset super.WriteLog("helper", 'Could not write to "#super.getTurbineProperty("ContextPath")#/app/utils/helper.cfc". Detail:#cfcatch.detail# Message:#cfcatch.Message#') />
		</cfcatch>
		</cftry>
	</cffunction>
	
	<!--- CONTROLLER --->
	<cffunction access="public" returntype="void" name="writeController">
		<cfset var qry_check_controller_base = "" />		
		<cfsavecontent variable="base_controller_generator"><cfinclude template="templates/base_controller_template.cfm"></cfsavecontent>
		<cfsavecontent variable="controller_generator"><cfinclude template="templates/controller_template.cfm"></cfsavecontent>
		
		<!--- read directory and check if base_controller exisits --->
		<cfdirectory action="list" directory="#super.getTurbineProperty('controllerpath')#" name="qry_check_controller_base" />
		<!--- do not overwrite this file as users may have custom code there --->
		<cfif (not listContainsNoCase(valueList(qry_check_controller_base.name),"base_controller.cfc"))>
			<cftry>
				<cffile action="write" file="#super.getTurbineProperty('controllerpath')#/base_controller.cfc" output="#trim(rereplace(rereplace(base_controller_generator,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
				<cfset super.WriteLog("base_controller", "Successfully written to '#super.getTurbineProperty('controllerpath')#/base_controller.cfc'")>
			<cfcatch type="any">
				<cfset super.WriteLog("base_controller", 'Could not write to "#super.getTurbineProperty("controllerpath")#/base_controller.cfc". Detail:#cfcatch.detail# Message:#cfcatch.Message#') />
			</cfcatch>
			</cftry>
		</cfif>
		
		<!--- ok to overwrite the individual controllers --->
		<cftry>
			<cffile action="write" file="#super.getTurbineProperty('controllerpath')#/#form.table_name#_controller.cfc" output="#trim(rereplace(rereplace(controller_generator,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cfset super.WriteLog("controller_#form.model_name#", "Successfully written to '#super.getTurbineProperty('controllerpath')#/#form.table_name#_controller.cfc'")>
		<cfcatch type="any">
			<cfset super.WriteLog("controller_#form.model_name#", 'Could not write to #super.getTurbineProperty("controllerpath")#/#form.table_name#_controller.cfc. Detail:#cfcatch.detail# Message:#cfcatch.Message#') />
		</cfcatch>
		</cftry>
	</cffunction>
	
	<!--- MODEL FACTORY,BASE MODEL, MODELS --->
	<cffunction access="public" returntype="void" name="writeModel">
		<cfset var qry_check_model_base = "" />
		
		<cfsavecontent variable="base_model_generator"><cfinclude template="templates/base_model_template.cfm"></cfsavecontent>
		<cfsavecontent variable="model_generator"><cfinclude template="templates/model_template.cfm"></cfsavecontent>
		
		<!--- read directory and check if base_controller exisits --->
		<cfdirectory action="list" directory="#super.getTurbineProperty('modelpath')#" name="qry_check_model_base" />
		<!--- do not overwrite this file as users may have custom code there --->
		<cfif (NOT listContainsNoCase(valueList(qry_check_model_base.name),"base_model.cfc"))>
			<cftry>
				<cffile action="write" file="#super.getTurbineProperty('modelpath')#/base_model.cfc" output="#trim(rereplace(rereplace(base_model_generator,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
				<cfset super.WriteLog("base_model", "Successfully written to base_model.cfc")>
			<cfcatch type="any">
				<cfset super.WriteLog("base_model", 'Could not write to "base_model.cfc". Detail:#cfcatch.detail# Message:#cfcatch.Message#') />
			</cfcatch>
			</cftry>	
		</cfif>
		
		<cftry>
			<cffile action="write" file="#super.getTurbineProperty('modelpath')#/#form.model_name#.cfc" output="#trim(rereplace(rereplace(model_generator,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cfset super.WriteLog("#form.model_name#-Model", "Successfully written to '#super.getTurbineProperty('modelpath')#/#form.model_name#.cfc'")>
		<cfcatch type="any">
			<cfset super.WriteLog("#form.model_name#-Model", 'Could not write to #super.getTurbineProperty("modelpath")#/#form.model_name#.cfc. Detail:#cfcatch.detail# Message:#cfcatch.Message#') />
		</cfcatch>
		</cftry>
	</cffunction>
	
	<!--- MODEL FACTORY --->
	<cffunction access="public" returntype="void" name="writeModelFactory">
		<cfargument name="template_name_and_path" default="templates/model_factory_template.cfm" type="String" />
		
		<cfsavecontent variable="model_factory_generator"><cfinclude template="#arguments.template_name_and_path#"></cfsavecontent>
		
		<cftry>
			<cffile action="write" file="#super.getTurbineProperty('modelpath')#/model_factory.cfc" output="#trim(rereplace(rereplace(model_factory_generator,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cfset super.WriteLog("ModelFactory", "Successfully written to '#super.getTurbineProperty('modelpath')#/model_factory.cfc'")>
		<cfcatch type="any">
			<cfset super.WriteLog("ModelFactory", 'Could not write to "#super.getTurbineProperty("modelpath")#/model_factory.cfc". Detail:#cfcatch.detail# Message:#cfcatch.Message#') />
		</cfcatch>
		</cftry>
	</cffunction>
	
	<!--- DAO --->
	<cffunction access="public" returntype="void" name="writeDAOFile">
		<cfargument name="dao_name" required="true" type="string" hint="Pass the 'form.table_name' here">
		<cfargument name="template_name_and_path" default="templates/dao_template.cfm" type="String" />
		
		<!--- Included template makes use of the FORM scope to write correct DAO. Cannot be used as a stand alone class --->
		<cfsavecontent variable="dao_generator"><cfinclude template="#arguments.template_name_and_path#"></cfsavecontent>
		<cftry>
			<cffile action="write" file="#super.getTurbineProperty('daopath')#/#arguments.dao_name#.cfc" output="#trim(rereplace(rereplace(dao_generator,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cfset super.WriteLog("DAO", "Successfully written to '#super.getTurbineProperty('daopath')#/#arguments.dao_name#.cfc'")>
		<cfcatch type="any">
			<cfset super.WriteLog("DAO", 'Could not write to "#super.getTurbineProperty('daopath')#/#arguments.dao_name#.cfc". Detail:#cfcatch.detail# Message:#cfcatch.Message#') />
		</cfcatch>
		</cftry>
	</cffunction>
	
	<!--- VIEWS --->
	<cffunction access="public" returntype="void" name="writeView">
		<cfinclude template="templates/view_template.cfm">
		
		<cftry>
			<cffile action="write" file="#super.getTurbineProperty('viewpath')#/#form.table_name#/index.cfm" output="#trim(rereplace(rereplace(indexContent,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cffile action="write" file="#super.getTurbineProperty('viewpath')#/#form.table_name#/new.cfm" output="#trim(rereplace(rereplace(newFormContentWrapper,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cffile action="write" file="#super.getTurbineProperty('viewpath')#/#form.table_name#/newContent.cfm" output="#trim(rereplace(rereplace(newFormContent,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cffile action="write" file="#super.getTurbineProperty('viewpath')#/#form.table_name#/show.cfm" output="#trim(rereplace(rereplace(showFormContent,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cffile action="write" file="#super.getTurbineProperty('viewpath')#/#form.table_name#/edit.cfm" output="#trim(rereplace(rereplace(editFormContentWrapper,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cffile action="write" file="#super.getTurbineProperty('viewpath')#/#form.table_name#/editContent.cfm" output="#trim(rereplace(rereplace(editFormContent,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cffile action="write" file="#super.getTurbineProperty('viewpath')#/#form.table_name#/update.cfm" output="#trim(rereplace(rereplace(updateActionContent,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cffile action="write" file="#super.getTurbineProperty('viewpath')#/#form.table_name#/delete.cfm" output="#trim(rereplace(rereplace(deleteActionContent,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cffile action="write" file="#super.getTurbineProperty('viewpath')#/#form.table_name#/create.cfm" output="#trim(rereplace(rereplace(createActionContent,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cfset super.WriteLog("view", 'Successfully written view files to folder') />
		<cfcatch type="any">
			<cfset super.WriteLog("view", 'Could not write view files to folder. Detail:#cfcatch.detail# Message:#cfcatch.Message#') />
		</cfcatch>
		</cftry>
	</cffunction>
	
	<!--- MIGRATIONS --->
	<cffunction access="public" returntype="void" name="writeMigrations">
		<cfsavecontent variable="migration_generator"><cfinclude template="templates/migration_template.cfm"></cfsavecontent>
	
		<cftry>
			<cffile action="write" file="#super.getTurbineProperty('migrationspath')#/#dateFormat(now(),'yyyy-mm-dd')#-#timeformat(now(),'HH-mm-ss')#_#form.table_name#.cfc" output="#trim(rereplace(rereplace(migration_generator,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cfset super.WriteLog("migrations", "Successfully written migrations to #super.getTurbineProperty('migrationspath')#/#dateFormat(now(),'yyyymmdd')##timeformat(now(),'hhmmss')#_#form.table_name#.cfc ") />
		<cfcatch type="any">
			<cfset super.WriteLog("migrations", 'Could not write migrations file. Detail:#cfcatch.detail# Message:#cfcatch.Message#') />
		</cfcatch>
		</cftry>
	</cffunction>
	
	<!--- MISC. FILES --->
	<cffunction access="public" returntype="void" name="writeEclipseProject">
		<cfsavecontent variable="eclipse_generator"><cfinclude template="templates/eclipse_project_template.cfm"></cfsavecontent>
	
		<cftry>
			<cffile action="write" file="#super.getTurbineProperty('ContextPath')#/.project" output="#trim(rereplace(rereplace(eclipse_generator,'\&lt;','<','ALL'),'\&gt;','>','ALL'))#" />
			<cfset super.WriteLog("eclipse_project", "Successfully written created an eclipse project file for your new application") />
		<cfcatch type="any">
			<cfset super.WriteLog("eclipse_project", 'Could not write .project file. Detail:#cfcatch.detail# Message:#cfcatch.Message#') />
		</cfcatch>
		</cftry>
	</cffunction>
	<cffunction access="public" returntype="void" name="writehtaccessFile">
<cfsavecontent variable="htaccess_content">
RewriteEngine on
RewriteBase <cfoutput>/#form.context_name#</cfoutput>
RewriteRule (^[/a-zA-Z0-9]+)$	index.cfm?$1	[L]
</cfsavecontent>

<cfsavecontent variable="httpd_ini">
RewriteEngine On
RewriteRule /#form.context_name#/([a-zA-Z0-9/.]*) /#form.context_name#/index.html?$1  [L]
</cfsavecontent>

		<cftry>
			<cffile action="write" file="#super.getTurbineProperty('ContextPath')#/.htaccess" output="#htaccess_content#" />
			<cffile action="write" file="#super.getTurbineProperty('ContextPath')#/httpd.ini" output="#httpd_ini#" />
			<cfset super.WriteLog("htaccess", "Successfully wrote to htaccess file for 'pretty urls'") />
			<cfset super.WriteLog("httpd.ini", "Successfully wrote to httpd.ini file for 'pretty urls'. Follow instructions for ISAPI_Rewrite 3") />
			<cfset super.WriteLog("isapi_rewrite", "Get the rewrite engine from <a href='http://www.google.com/url?sa=t&source=web&ct=res&cd=1&ved=0CAoQFjAA&url=http%3A%2F%2Fwww.helicontech.com%2Fdownload%2Fisapi_rewrite%2FISAPI_Rewrite3_0067_Lite.msi&ei=RnMhS-S7BMnNlQfsycz7CQ&usg=AFQjCNGzbSYrzozYntkrFeQuLHIfnBZEtQ&sig2=o4AiTMlLoQuRhD_dv9TEeA'>here</a>") />
		<cfcatch type="any">
			<cfset super.WriteLog("pretty_urls", 'Could not write .htaccess or httpd.ini file. Detail:#cfcatch.detail# Message:#cfcatch.Message#') />
		</cfcatch>
		</cftry>
			
	</cffunction>
	
</cfcomponent>